package com.yixuan.mt.console.controller;

import com.yixuan.mt.common.entity.Bucket;
import com.yixuan.mt.common.exception.HttpBadRequestException;
import com.yixuan.mt.common.response.Result;
import com.yixuan.mt.common.service.BucketsService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/op/buckets")
public class BucketsController {

    @Autowired
    BucketsService bucketsService;

    /**
     * 创建 bucket
     * @param bucketName 桶名
     */
    @PostMapping
    public Result<?> postBuckets(@RequestBody String bucketName) throws IOException {
        bucketsService.postBuckets(bucketName);
        return Result.success();
    }

    /**
     * 删除 bucket
     * @param bucketName 桶名
     * @throws HttpBadRequestException 如果桶中有对象，则抛出异常。（这同时也代表更新数据库中桶的对象数应该在往数据库插入对象之前）
     */
    @DeleteMapping("/{bucketName}")
    public Result<?> deleteBuckets(@PathVariable String bucketName) {
        bucketsService.deleteBuckets(bucketName);
        return Result.success();
    }

    /**
     * 更新 bucket 信息
     */
    @PutMapping
    public Result<?> putBuckets(@Valid @RequestBody Bucket bucket) {
        System.out.println(bucket);
        bucketsService.putBuckets(bucket);
        return Result.success();
    }

    /**
     * 获取 bucket 列表
     */
    @GetMapping
    public Result<List<Bucket>> getBuckets() throws IOException {
        return Result.success(bucketsService.getBuckets());
    }

    /**
     * 获取 bucket 的 summary
     */
    @GetMapping("/summary/{bucketName}")
    public Result<Bucket> getBucketsSummary(@PathVariable String bucketName) {
        return Result.success(bucketsService.getBucketsSummary(bucketName));
    }
}
